/****************************************************************************
**
** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies).
** Contact: http://www.qt-project.org/legal
**
** This file is part of the examples of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:BSD$
** You may use this file under the terms of the BSD license as follows:
**
** "Redistribution and use in source and binary forms, with or without
** modification, are permitted provided that the following conditions are
** met:
**   * Redistributions of source code must retain the above copyright
**     notice, this list of conditions and the following disclaimer.
**   * Redistributions in binary form must reproduce the above copyright
**     notice, this list of conditions and the following disclaimer in
**     the documentation and/or other materials provided with the
**     distribution.
**   * Neither the name of Digia Plc and its Subsidiary(-ies) nor the names
**     of its contributors may be used to endorse or promote products derived
**     from this software without specific prior written permission.
**
**
** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
**
** $QT_END_LICENSE$
**
****************************************************************************/

#include <QComboBox>

#include "colorlisteditor.h"

ColorListEditor::ColorListEditor(EProfile *profile, int editIndex)
{
    _id = editIndex;
    QColor *colors = profile->colors();

        QStringList colorNames = (QStringList() << "Black" << "Red" << "Green"
         << "Yellow" << "Blue" << "Magenta" << "Cyan" << "White" << "Bold Black"
         << "Bold Red" << "Bold Green" << "Bold Yellow" << "Bold Blue"
         << "Bold Magenta" << "Bold Cyan" << "Bold White");
    int i, matchIndex = -1;
    /* editIndex is the index of the color that's being edited. If it matches
       any ansi colors, show that color. If not, show the default color.
       Non-ansi colors can refer to ansi colors, and nothing else. This gives
       the user to say they want something to be 'whatever yellow is', and not
       have to worry about it. */
    QColor editColor = colors[editIndex];

    for (i = 0;i < colorNames.size();i++) {
        QColor color = colors[i];
        if (color == editColor)
            matchIndex = i;

        insertItem(i, colorNames.at(i));
        setItemData(i, color, Qt::DecorationRole);
    }
    delete colors;

    /* Add a 'custom color' option at the top. If this is selected, then the
       editor will make a color picking button appear. */
    QColor custom = QColor(0, 0, 0, 0);
    insertItem(0, "Custom Color");
    setItemData(0, custom, Qt::DecorationRole);

    _usingAnsiColor = (matchIndex != -1);

    /* +1 because of the custom color. */
    if (_usingAnsiColor)
        setCurrentIndex(matchIndex + 1);
    else
        setCurrentIndex(0);

    connect(this, SIGNAL(currentIndexChanged(int)), this,
            SLOT(currentIndexChanged(int)));
}

void ColorListEditor::currentIndexChanged(int newIndex)
{
    emit pickerIndexChanged(_id, newIndex);
}

QColor ColorListEditor::color() const
{
    QVariant v = itemData(currentIndex(), Qt::DecorationRole);
    return v.value<QColor>();
}

void ColorListEditor::setColor(QColor color)
{
    setCurrentIndex(findData(color, int(Qt::DecorationRole)));
}
